package com.ogawa.joypal.user.activity

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseVMActivity
import com.ogawa.base.utils.TimeUtils
import com.ogawa.base.utils.imageloader.ImageConfigImpl
import com.ogawa.base.utils.imageloader.ImageLoader
import com.ogawa.base.utils.printNoNull
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil
import com.ogawa.joypal.user.R
import com.ogawa.joypal.user.bean.PutUserInfoRequest
import com.ogawa.joypal.user.viewmodel.UserInfoRegisterViewModel
import com.ogawa.joypal.user.widget.DatePickDialog
import com.ogawa.joypal.user.widget.ValuePickDialog
import com.ogawa.projectcommon.constants.Constant
import com.ogawa.projectcommon.utils.ConstantStringUtils
import com.ogawa.projectcommon.utils.ProjectSpUtils
import kotlinx.android.synthetic.main.activity_user_register.*


@Route(path = UrlPathConstant.ACTIVITY_USER_REGISTER)
public class UserRegisterActivity : BaseVMActivity<UserInfoRegisterViewModel>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_user_register
    }

    private var genderDialog: ValuePickDialog? = null
    private var datePickDialog: DatePickDialog? = null

    override fun initView() {
        super.initView()
        SystemStatusBarUtil.setStatusBar(this, R.color.colorWhite, true)
        et_sex.setOnClickListener {
            showGenderDialog()
        }

        et_select_birthday.setOnClickListener {
            showDateDialog()
        }
        tv_skip.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
                .navigation()
        }
        tv_next.setOnClickListener {
            mViewModel.putUserInfo()
        }

        val userInfo = ProjectSpUtils.getUserInfo()
        if (userInfo != null) {
            mViewModel.userInfo("${userInfo.user.id}")
        }

    }


    private fun showGenderDialog() {
        if (genderDialog == null) {
            genderDialog = ValuePickDialog(this, ValuePickDialog.GENDER)
            genderDialog!!.setTvSaveOnClickListener {
                val sex = (genderDialog!!.valueIndex + 1) % 2
//                var gender = sex.toString()
                var gender = genderDialog!!.value
                et_sex.setText(gender)
                mViewModel.mPutUserInfoRequest.sex = "$sex"
                genderDialog!!.dismiss()
            }
        }
        genderDialog!!.show()
    }

    private fun showDateDialog() {
        if (datePickDialog == null) {
            datePickDialog = DatePickDialog(this)
            datePickDialog!!.setTvSaveOnClickListener {
                val year = datePickDialog!!.year
                val month = datePickDialog!!.month
                val day = datePickDialog!!.day
                et_select_birthday.setText(TimeUtils.printDateStr("$year", "$month", "$day"))
                var birLong = TimeUtils.ydmTo13Long("$year", "$month", "$day")
                mViewModel.mPutUserInfoRequest.birthDate = birLong
                datePickDialog!!.dismiss()
            }
        }
        datePickDialog!!.show()
    }

    override fun providerVMClass(): Class<UserInfoRegisterViewModel>? {
        return UserInfoRegisterViewModel::class.java
    }

    override fun observeVM() {
        mViewModel.putUserInfoState.observe(this, Observer {
            if (it == Constant.SUCCESS) {
                ToastUtils.showShort(getString(R.string.save_success))
                finish()
                ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
                    .navigation()
                return@Observer
            }
        })

        mViewModel.mUserInfosBean.observe(this, Observer {
            it?.run {
                mViewModel.mPutUserInfoRequest = PutUserInfoRequest()
                mViewModel.mPutUserInfoRequest.userId = it.id
                mViewModel.mPutUserInfoRequest.nickName =
                    if (it.nickName != null) "${it.nickName}" else ""
                mViewModel.mPutUserInfoRequest.sex = "${it.sex}"
                mViewModel.mPutUserInfoRequest.height = "${it.height}"
                mViewModel.mPutUserInfoRequest.weight = "${it.weight}"
                mViewModel.mPutUserInfoRequest.birthDate = it.birthDate
                mViewModel.mPutUserInfoRequest.massageExperience = "${it.massageExperience}"
                mViewModel.mPutUserInfoRequest.tolerance = "${it.tolerance}"
                mViewModel.mPutUserInfoRequest.image = "${it.image}"
            }


        })

    }


}




